"""
Case Type   : 资源池化
Case Name   : 系统函数dss_io_stat函数字段及功能验证(备节点只读业务)
Create At   : 2024.6
Owner       : haomeng
Description :
    1、备节点执行只读业务
    2、备节点查看IO读写速度,时间间隔为1s,2s,10s,30s,60s
    3、主节点查看IO读写速度，时间间隔为1s,2s,10s,30s,60s
    4、停止业务，恢复环境
Expect      :
    1、业务运行成功
    2、查看成功，无异常返回,写入数据速度为0
    3、查看成功，无异常返回,读写数据速度均为0
    4、停止业务成功，恢复环境成功
"""


import os
import time
import unittest
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import macro
from yat.test import Node


class SharedStorage(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.sta_sh = CommonSH('Standby1DbUser')
        self.root_usernode = Node('Standby1Root')
        self.constant = Constant()
        self.com = Common()
        self.sta_pg = 'sta_props.pg'
        self.sta_tpcc_log = 'sta_tpcc_result.log'

        self.log.info('------检查数据库状态是否正常------')
        status = self.pri_sh.exec_cm_ctl('query', '-Cv')
        self.assertIn("cluster_state   : Normal", status)

    def test_innerfunc(self):
        text = '-----step1:备节点开启tpcc只读业务; expect:业务执行成功-----'
        self.log.info(text)

        sta_tpcc = self.com.startTPCC(self.root_usernode,
                                      self.sta_pg,
                                      self.sta_tpcc_log)
        self.log.info(sta_tpcc)
        time.sleep(30)
        sta_res = self.root_usernode.sh(f'cat {macro.TPCC_PATH}/{self.sta_tpcc_log}').result()
        self.log.info(sta_res)
        self.assertIn('Current tmpTOTAL', sta_res, 'tpcc业务执行失败')

        text = '-----step2:备节点查看IO读写速度,时间间隔为time_list; ' \
               'expect:查看成功，无异常返回,写入数据速度为0-----'
        self.log.info(text)

        time_list = [1, 2, 10, 30, 60]
        for s_time in time_list:
            sql = f'select * from dss_io_stat({s_time});'
            self.log.info(sql)
            res1 = self.sta_sh.execut_db_sql(sql)
            self.log.info(res1)
            r_res = res1.splitlines()[-2].split('|')[0].strip()
            w_res = res1.splitlines()[-2].split('|')[1].strip()
            self.assertNotEqual('0', r_res, '备节点查看读取速度为0')
            self.assertEqual('0', w_res, '备节点查看写入速度不为0')

        text = '-----step3:主节点查看IO读写速度,时间间隔为time_list; ' \
               'expect:查看成功，无异常返回,读写数据速度均为0-----'
        self.log.info(text)

        for p_time in time_list:
            sql = f'select * from dss_io_stat({p_time});'
            self.log.info(sql)
            res2 = self.pri_sh.execut_db_sql(sql)
            self.log.info(res2)
            r_res = res2.splitlines()[-2].split('|')[0].strip()
            w_res = res2.splitlines()[-2].split('|')[1].strip()
            self.assertEqual('0', r_res, '主节点查看写入速度不为0')
            self.assertEqual('0', w_res, '主节点查看读取速度不为0')

    def tearDown(self):
        text = '-----step4:恢复集群初始状态; expect:恢复初始状态成功-----'
        self.log.info(text)

        self.log.info('-----停止tpcc业务-----')
        tpcc_pid = self.com.get_pid(self.root_usernode,
                                    f'"{self.sta_pg}"')
        self.log.info(tpcc_pid)
        stop_tpcc = self.root_usernode.sh(f'kill -9 {tpcc_pid}').result()
        self.log.info(stop_tpcc)

        start_dn = self.pri_sh.exec_cm_ctl('cm_ctl', 'start')
        self.log.info(start_dn)

        recovery_dn = self.pri_sh.exec_cm_ctl('switchover', '-a')
        self.log.info(recovery_dn)
        self.assertIn(self.constant.cm_start_node_success_msg, start_dn,
                      '拉起集群失败')
        self.assertIn(self.constant.cm_switchover_success_msg, recovery_dn,
                      '恢复集群初始状态失败')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')

